package com.powernode.controller;

import com.powernode.domain.SysMenu;
import com.powernode.model.Result;
import com.powernode.service.SysMenuService;
import com.powernode.util.AuthUtils;
import com.powernode.vo.MenuAndAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName SysMenuController
 * @Description 系统权限控制层
 * @Date 2024/07/21 22:18
 */
@Api("系统权限接口管理")
@RequestMapping("/sys/menu")
@RestController
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    /**
     * 查询用户的菜单权限和操作权限
     * @return 返回菜单权限和操作权限
     */
    @ApiOperation("查询用户的菜单权限和操作权限")
    @GetMapping("/nav")
    public Result<MenuAndAuth> loadUserMenuAndAuth() {
        // 获取用户信息
        Long loginUserId = AuthUtils.getLoginUserId();
        // 查操作权限
        Set<String> loginUserPerms = AuthUtils.getLoginUserPerms();
        // 查菜单权限
        Set<SysMenu> menus = sysMenuService.queryUserMenuListByUserId(loginUserId);
        MenuAndAuth menuAndAuth = MenuAndAuth.builder()
                .authorities(loginUserPerms)
                .menuList(menus)
                .build();
        return Result.success(menuAndAuth);
    }

    /**
     * 查询系统所有权限集合
     * @return 返回系统所有权限集合
     */
    @ApiOperation("查询系统所有权限集合")
    @GetMapping("/table")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<List<SysMenu>> loadAllSysMenuList() {
        List<SysMenu> menuList = sysMenuService.queryAllSysMenuList();
        return Result.success(menuList);
    }

    /**
     * 新增权限
     * @param sysMenu 权限对象
     * @return 返回新增结果
     */
    @ApiOperation("新增权限")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public Result<String> saveSysMenu(@RequestBody SysMenu sysMenu) {
        return Result.handle(sysMenuService.save(sysMenu));
    }

    /**
     * 查询权限详情
     * @param menuId 权限id
     * @return 返回权限详情
     */
    @ApiOperation("查询权限详情")
    @GetMapping("/info/{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:info')")
    public Result<SysMenu> loadSysMenuInfo(@PathVariable Long menuId) {
        SysMenu menu = sysMenuService.getById(menuId);
        return Result.success(menu);
    }

    /**
     * 修改权限
     * @param sysMenu 权限对象
     * @return 返回修改结果
     */
    @ApiOperation("修改权限")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public Result<String> modifySysMenu(@RequestBody SysMenu sysMenu) {
        return Result.handle(sysMenuService.updateById(sysMenu));
    }

    /**
     * 删除权限
     * @param menuId 权限id
     * @return 返回删除结果
     */
    @ApiOperation("删除权限")
    @DeleteMapping("/{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public Result<String> removeSysMenu(@PathVariable Long menuId) {
        return Result.handle(sysMenuService.removeById(menuId));
    }
}
